
import javax.swing.*;
import java.awt.*;

public class Main {

    JFrame main_frame;
    JButton[][] buttons;
    Register registerLabel;
    AnswerLabel answerLabel;
    Container c;

    void setButtons()
    {
        buttons = new JButton[6][4];

        for(int i = 0; i < 6; i ++)
        {
            for(int j = 0; j < 4; j ++)
            {
                buttons[i][j] = new JButton();
                buttons[i][j].setBounds(20 + j * 90, 140 + i * 60, 90, 60);
            }
        }

        buttons[0][0].setText("%");
        buttons[0][0].addActionListener(new OperationListener(registerLabel, answerLabel, '%'));

        buttons[0][1].setText("CE");
        buttons[0][1].addActionListener(new CEListener(answerLabel));

        buttons[0][2].setText("C");
        buttons[0][2].addActionListener(new CListener(registerLabel, answerLabel));

        buttons[0][3].setText("Backspace");
        buttons[0][3].addActionListener(new BackspaceListener(answerLabel));

        buttons[1][0].setText("pow(-1)");
        buttons[1][0].addActionListener(new SqrListener(registerLabel, answerLabel, SqrListener.POWN1));

        buttons[1][1].setText("pow(2)");
        buttons[1][1].addActionListener(new SqrListener(registerLabel, answerLabel, SqrListener.SQ));

        buttons[1][2].setText("pow(0.5)");
        buttons[1][2].addActionListener(new SqrListener(registerLabel, answerLabel, SqrListener.SQRT));

        buttons[1][3].setText("/");
        buttons[1][3].addActionListener(new OperationListener(registerLabel, answerLabel, '/'));

        buttons[2][0].setText("7");
        buttons[2][0].addActionListener(new DigitListener(answerLabel, '7'));

        buttons[2][1].setText("8");
        buttons[2][1].addActionListener(new DigitListener(answerLabel, '8'));

        buttons[2][2].setText("9");
        buttons[2][2].addActionListener(new DigitListener(answerLabel, '9'));

        buttons[2][3].setText("*");
        buttons[2][3].addActionListener(new OperationListener(registerLabel, answerLabel, '*'));

        buttons[3][0].setText("4");
        buttons[3][0].addActionListener(new DigitListener(answerLabel, '4'));

        buttons[3][1].setText("5");
        buttons[3][1].addActionListener(new DigitListener(answerLabel, '5'));

        buttons[3][2].setText("6");
        buttons[3][2].addActionListener(new DigitListener(answerLabel, '6'));

        buttons[3][3].setText("-");
        buttons[3][3].addActionListener(new OperationListener(registerLabel, answerLabel, '-'));

        buttons[4][0].setText("1");
        buttons[4][0].addActionListener(new DigitListener(answerLabel, '1'));

        buttons[4][1].setText("2");
        buttons[4][1].addActionListener(new DigitListener(answerLabel, '2'));

        buttons[4][2].setText("3");
        buttons[4][2].addActionListener(new DigitListener(answerLabel, '3'));

        buttons[4][3].setText("+");
        buttons[4][3].addActionListener(new OperationListener(registerLabel, answerLabel, '+'));

        buttons[5][0].setText("+/-");
        buttons[5][0].addActionListener(new SqrListener(registerLabel, answerLabel, SqrListener.NEGATE));

        buttons[5][1].setText("0");
        buttons[5][1].addActionListener(new DigitListener(answerLabel, '0'));

        buttons[5][2].setText(".");
        buttons[5][2].addActionListener(new DigitListener(answerLabel, '.'));

        buttons[5][3].setText("=");
        buttons[5][3].addActionListener(new OperationListener(registerLabel, answerLabel, '\0'));

        for(int i = 0; i < 6; i ++)
        {
            for(int j = 0; j < 4; j ++)
            {
                c.add(buttons[i][j]);
            }
        }
    }

    public Main() {

        main_frame = new JFrame();
        main_frame.setSize(400, 600);
        main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        c = main_frame.getContentPane();
        c.setLayout(null);

        registerLabel = new Register();
        registerLabel.setBounds(20, 20, 360, 40);
        c.add(registerLabel);

        answerLabel = new AnswerLabel();
        answerLabel.setBounds(20, 60, 360, 80);
        c.add(answerLabel);

        setButtons();

        main_frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}
